/*
    This file is part of Starlight Engine.

    Starlight Engine is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 2 of the License, or
    (at your option) any later version.

    Starlight Engine is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Starlight Engine.  If not, see <http://www.gnu.org/licenses/>.
*/

using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;
using StarlightEngine;

namespace StarlightEngine
{
    /// <summary>
    /// The engines base object, all game objects are derived.
    /// </summary>
    public class Node
    {

        Color drawColor = Color.White;
        /// <summary>
        /// Gets or sets the color of the sprite.
        /// </summary>
        /// <value>The color of the sprite.</value>
        public Color DrawColor
        {
            get { return drawColor; }
            set { drawColor = value; }
        }

        Vector2 origin = Vector2.Zero;
        /// <summary>
        /// Gets or sets the origin.
        /// </summary>
        /// <value>The origin.</value>
        public Vector2 Origin
        {
            get { return origin; }
            set { origin = value; }
        }

        Texture2D texture;
        string sText;
        /// <summary>
        /// Gets or sets the texture.
        /// </summary>
        /// <value>The texture.</value>
        public string Texture
        {
            get { return sText; }
            set
            {
                texture = TextureManager.GetTexture(value);
                sText = value;
                this.CreateRectangle();
            }
        }

        protected Texture2D TextureObject
        {
            get { return texture; }
        }

        /// <summary>
        /// Size to show in debug mode. Over-ridden in sprite-based nodes.
        /// </summary>
        private const int Size = 40;
        internal string addedAs = "";
        public string NameInScene
        {
            get { return addedAs; }
            set
            {                
                //Make scene redo node names.
                SceneManager.Scenes[inScene].RenameNode(addedAs, value);
                addedAs = value;
            }
        }

        internal string inScene = "";
        public string AddedToScene
        {
            get { return inScene; }
        }

        public Rectangle Rectangle
        {
            get { return this.rectangle; }
            set { this.rectangle = value; }
        }

        Vector2 pos = Vector2.Zero;
        /// <summary>
        /// Gets or sets the position.
        /// </summary>
        /// <value>The position.</value>
        public Vector2 Position
        {
            get { return pos; }
            set
            {
                pos = value;
                UpdateRectanglePosition();
            }
        }

        Rectangle rectangle;

        float scale = 1f;
        /// <summary>
        /// Gets or sets the scale.
        /// </summary>
        /// <value>The scale.</value>
        public float Scale
        {
            get { return scale; }
            set
            {
                scale = value;
                CreateRectangle();
            }
        }

        float rot = 0f;
        /// <summary>
        /// Gets or sets the rotation.
        /// </summary>
        /// <value>The rotation.</value>
        public float Rotation
        {
            get { return rot; }
            set { rot = value; }
        }


        /// <summary>
        /// Initializes a new instance of the <see cref="Node"/> class.
        /// </summary>
        public Node()
        {
            Texture = "Blank";
        }

        /// <summary>
        /// Updates this instance.
        /// </summary>
        public virtual void Update()
        {
            //TODO
            //What is needed?
        }

        /// <summary>
        /// Draws this instance.
        /// </summary>
        public virtual void Draw()
        {
            
        }

        /// <summary>
        /// Creates the rectangle.
        /// </summary>
        protected virtual void CreateRectangle()
        {
            rectangle = new Rectangle((int)pos.X, (int)pos.Y, (int)(Size * scale), (int)(Size * scale));
        }

        protected void UpdateRectanglePosition()
        {
            rectangle.X = (int)pos.X;
            rectangle.Y = (int)pos.Y;
        }
        
    }
}